package com.swh.servic;

import com.swh.dao.UserDao;
import com.swh.entity.PageEntity;
import com.swh.entity.User;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/21 18:21
 * @Version: 1.0
 */
public class UserServic {
    private UserDao userDao = new UserDao();

    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    public User findByUserName(String username) {
        return userDao.findByUserName(username);
    }

    //注册
    public void register(User user) {
        user.setRegisterTime(new Date());
        user.setPic("http://localhost:8080/upload/def.png");
        user.setIsSecret("0");
        user.setRealName(user.getUsername());
        userDao.save(user);
    }

    //查询所有
    public List<User> findAll() {
        return userDao.findAll();
    }


    //查询所有带添加查询
    public List<User> search(String realName) {
        return userDao.search(realName);
    }

    //查询所有带添加+分页
    public PageEntity<User> findPage(String realName, Integer currPage) {
        PageEntity<User> pageEntity = new PageEntity<>();

        // 页大小
        Integer pageSize = pageEntity.getPageSize();

        // 查询当前页的数据
        Integer startIndex = (currPage - 1) * pageSize;

        // 当前页的数据
        List<User> userList = userDao.findPage(realName, startIndex, pageSize);

        // 总记录数
        Integer totalCount = userDao.countByRealName(realName);

        // 总页数
        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;


        pageEntity.setData(userList);
        pageEntity.setTotalCount(totalCount);
        pageEntity.setTotalPage(totalPage);
        pageEntity.setCurrPage(currPage);


        return pageEntity;
    }

    public List<Map<String, Object>> findUserByDeptId(Long deptId) {
        return userDao.findUserByDeptId(deptId);
    }

    public User findById(Long id) {
        return userDao.findById(id);
    }

    //查看数
    public void incLook(Long id) {
        userDao.incLook(id);
    }

    //查询粉丝数
    public Integer findFans(Long id) {
        return userDao.findFans(id);
    }

    //关注数
    public Integer findFocus(Long id) {
        return userDao.findFocus(id);
    }

    //修改图片地址
    public void updatePic(Long id, String path) {
        userDao.updatePic(id, path);
    }


    public void update(User user) {
        userDao.update(user);
    }

    //查看关注那些人
    public List<Long> findFocusListByUserId(Long id) {
        return userDao.findFocusListByUserId(id);
    }

    //加关注或取消关注
    public Integer focus(Long userId, long focusId) {

        List<Long> focusList = userDao.findFocusListByUserId(userId);

        if (focusList != null && focusList.contains(focusId)) {
            //取消关注
            userDao.delFocus(userId, focusId);
            return 0;
        }
        //加关注
        userDao.addFocus(userId, focusId);
        return 1;
    }


    /**
     * 根据userId查询关注列表
     *
     * @param userId
     * @param currPage
     * @return
     */
    public PageEntity<Map<String, Object>> findFocusPage(Long userId, Integer currPage) {

        PageEntity<Map<String, Object>> pageEntity = new PageEntity<>();
        Integer pageSize = pageEntity.getPageSize();

        // 总记录数(我的关注数)
        Integer totalCount = userDao.findFocus(userId);

        // 总页数
        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

        Integer startIndex = (currPage - 1) * pageSize;

        // 当前页数据
        List<Map<String, Object>> data = userDao.findFocusPage(userId, startIndex, pageSize);

        pageEntity.setCurrPage(currPage);
        pageEntity.setTotalCount(totalCount);
        pageEntity.setTotalPage(totalPage);
        pageEntity.setData(data);

        return pageEntity;
    }


    // 更改用户登录时间
    public void updateLoginTime(Long id) {
        userDao.updateLoginTime(id);
    }


    //微信登录
    public User findByWxOpenId(String openid) {
        return userDao.findByWxOpenid(openid);
    }

    //QQ登录
    public User findByQqOpenid(String openid) {
        return userDao.findByQqOpenid(openid);
    }

    //查询数据库中最大id
    public Long findMaxId() {
        return userDao.findMaxId();
    }

    //添加QQ微信登录用户
    public void save(User user) {
        //默认私密
        user.setIsSecret("0");
        //注册当前时间
        user.setRegisterTime(new Date());

        userDao.save(user);
    }
}
